package controller;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

@SuppressWarnings("serial")
public class TranslateServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		String content="";
		String language = request.getParameter("language");
		String urls = request.getParameter("url");
		
		String link = "http://translate.google.com/translate?hl=en&rurl=translate.google.com&sl=auto&tl="+language+"&twu=1&u="+urls;
		response.getWriter().println(link);
		String charset="utf-8";
		URL url;
		System.out.println(link);
		try {
			content = Utils.GetHtml(link, charset);
			
			Document doc = Jsoup.parse(content);
			
			link = "http://translate.google.com"+doc.select("iframe").attr("src");
			String location = "";
			try {
				url = new URL(link);
				HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			    connection.setInstanceFollowRedirects(true);
			    connection.setReadTimeout(5000000);
			    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 AlexaToolbar/alxf-2.14 AlexaToolbar/pXVUnvuf-1.2 Firefox/3.6.21");
				connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
				connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
				connection.setRequestProperty("Accept-Charset", "	ISO-8859-1,utf-8;q=0.7,*;q=0.7");
				connection.setRequestProperty("Connection", "keep-alive");
				connection.setRequestProperty("Keep-Alive", "115");
				connection.setRequestProperty("Cookie", "PREF=ID=987bb7a56b1cc7bc:U=691fa246d56f502e:FF=0:TM=1314876638:LM=1314876681:S=yBXzqp_WQq9ykX-j; NID=50=nfNXg7ymSSsfdzMnkEuo7t1GCgIEwcK5xmpFhu1mQWwvNI5nFHLZpRsy_1XnaoKrdj9hnz7za9-M2OIO99phfFSTmXln-xCvqzJaRWHsN0EQKgpmkvgUDTWPVF4I4c9L; SID=DQAAALcAAADhqr-J8Ls3CKhmbRjwTMMP1DjmIvR8qBFxv99saTaFFgoRsVf18W4p_BTocYoxYNMvsvh9sfjMj7KqLyd_s9Y0lwYY_Rrljd7t5IpKBTWWnsv9VSJ_qelH943zV8YIT3uuIdJbHhFlCnVEu_YealZoldWjf4VirYR0PIrr0i4_JU2JCHRmpyW0ACt3KcyLkukJlGDyT5yC5js3CBSTAhnWSylBpMWPWyWJzqXe2BQFcdfuU_qo00pAR-uEcMQ437M; HSID=A9Z-j3CKhzDEykAES");
				connection.setRequestProperty("Cache-Control", "max-age=0");
				BufferedReader reader=null;
				
			    reader= new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName(charset)));
			    
		        reader.close();
		        location = connection.getHeaderField("Location");
		        response.getWriter().println(location);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
			content = Utils.GetHtml(location, "utf-8");
	        
		}  catch (Exception e) {
			
			e.printStackTrace();
		}
		response.getWriter().println(content);

	}
}
